<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/*
 *      Media.php
 *      
 *      Copyright 2011 godfather.typhoon.tycoon@banme <chungnguyenhuu911@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 */

	class  Picture   extends  CI_Controller{
		private $pro_id;
		private $id;
		private $data = array(
				'dir' =>  'public/uploads/image/products/pictures/',
				'total' => 0,
				'images' => array(),
				'error' => ''
			);
		function  Picture  (){
			
			}
		function __construct(){
			parent::__construct();
			$this->load->library('pagination');
			//$this->load->library('form_validation');
			$this->lang->load('messages','vietnamese');
			/* ---------------------------------------------------
			 * Check admin login
			 *  
			*/ 

			if(!$this->session->userdata('go_admin')){
				redirect('admin/login', 'refesh');
				return;
			}
		}
		function index($start = 0){
			
			/*
			 * validate
			 */ 
			 
			
			$this->pro_id = $this->uri->segment(3);
			
			
			$this->form_validation-> set_rules('name', 'Name', 'required|xss_clean');
			$this->form_validation-> set_rules('name_en', 'Name En', 'required|xss_clean');
			
			$this->data['pro_id'] = $this->pro_id;
			$config['base_url'] = site_url('picture/index/'.$this->pro_id);
			
			$config['per_page'] = $this->config->item('admin.product.picture.page');
			$config['total_rows'] = $this->count($this->pro_id);
			
			$config['uri_segment'] = '4';
			
			$this->data['images'] = $this->findByProductID($this->pro_id, $config['per_page'], $this->uri->segment(4));
			$this->pagination->initialize($config);
			
			if ($this->form_validation->run() == FALSE){
				$this->data['main_content'] = 'manager/picture';
				$this->load->view('templates/manager/template', $this->data);
			}else{
				
				$name = $this->input->post('name');
				$name_en = $this->input->post('name_en');
				$url = "";
				$pro_id = $this->input->post('pro_id');
				/*
				 * 
				 * Upload file
				 * 
				 */ 
				
				//get file config
				
				$path = $this->config->item('product.picture');
				
				$config['upload_path'] = $path;
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '1024';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';
				

				$this->load->library('upload', $config);

				if ( ! $this->upload->do_upload())
				{
					$this->data['error'] = $this->upload->display_errors();
					$this->data['main_content'] = 'manager/picture';
					$this->load->view('templates/manager/template', $this->data);
					
					return;
				}
				else
				{
					$data =  $this->upload->data();
					$url = $data['file_name'];
					$this->insert($name, $name_en, $url, $pro_id);
					$this->data['main_content'] = 'manager/picture/'. $pro_id;
					$this->load->view('templates/manager/template', $this->data);
					//redirect('picture/index/'.$pro_id, 'refesh');
				}
			}
		}
		function findByProductID($id = -1, $limit = NULL, $offset = NULL){
			$query = $this->db->get_where('image', array('product_id' => $id), $limit, $offset);
			return $query->result();

		}
		function count($id = -1){
			$this->db->where('product_id', $id); 
			$this->db->from('image');
			return $this->db->count_all_results();
			
		}
		function delete(){
		
			if($this->uri->segment(3) == "del" && $this->uri->segment(5) != "" && $this->uri->segment(4)){
				$pro_id =  $this->uri->segment(4);
				$id = $this->uri->segment(5);
				
				$picture = $this->findByID($id);
				
				$this->remove($id, $picture['url']);

				$config['base_url'] = site_url('picture/index/'.$pro_id);
			
				$config['per_page'] = 2;
				
				$config['total_rows'] = $this->count($pro_id);
				
				$config['uri_segment'] = '4';
			
				$this->data['images'] = $this->findByProductID($pro_id, $config['per_page'], $id);
				$this->pagination->initialize($config);
				redirect('picture/index/'.$pro_id, 'refesh');
			 }
		}
		
		function edit(){
			$data = array();
			/*
			 * 
			 * redirect from list module
			 * Get ID
			 */ 
			 
			$id = $this->uri->segment(4);
			
			/*
			 * Check id
			 * if GET id param is null or empty
			 * Get  POST id param
			 */ 
			if($id == null || $id ==""){
				$id = $this->input->post('id');
			}
			$pro_id =  $this->uri->segment(3);
			if($pro_id == null || $pro_id ==""){
				$pro_id = $this->input->post('pro_id');
			}
			
			$this->form_validation-> set_rules('name', 'Name', 'required|xss_clean');
			$this->form_validation-> set_rules('name_en', 'Name En', 'required|xss_clean');

			$data['pro_id'] = $pro_id;
			$data['picture'] = $this->findByID($id);
			if ($this->form_validation->run() == FALSE){
				$data['main_content'] = 'manager/editpicture';
				$this->load->view('templates/manager/template', $data);
			}else{
				$id = $this->input->post('id');
				$name = $this->input->post('name');
				$name_en = $this->input->post('name_en');
				$url = $data['picture']['url'];
				$product_id = $pro_id;
				/*
				 * Upload image
				 * //check has image
				 */ 
				 if($_FILES['userfile']["size"] <= 0 ){
					$url = $data['picture']['url'];
				}else{
					$path = $this->config->item('product.picture');
					$config['upload_path'] = $path;
					$config['allowed_types'] = 'gif|jpg|png';
					$config['max_size']	= '1024';
					$config['max_width']  = '1024';
					$config['max_height']  = '768';
					$this->load->library('upload', $config);

					if ( ! $this->upload->do_upload())
					{
						$data['error'] = $this->upload->display_errors();
						$url = $data['picture']['url'];
						$data['main_content'] = 'manager/editpicture';
						$this->load->view('templates/manager/template', $data);
						return;
					}
					else
					{
						//delete old image
						$this->removieImage($data['picture']['url']);
						
						$data =  $this->upload->data();
						$url = $data['file_name'];
						$this->update($name, $name_en, $url, $product_id, $id);
						$this->data['main_content'] = 'manager/picture/'. $pro_id;
						$this->load->view('templates/manager/template', $this->data);
						//redirect('picture/index/'.$pro_id, 'refesh');
					}
				}
				$this->data['main_content'] = 'manager/picture/'. $pro_id;
				$this->load->view('templates/manager/template', $this->data);
				return;
			}
		}
		function insert($name = "", $name_en = "", $url = "", $pro_id = -1){
			$sql = "INSERT INTO  `image` (name, name_en, url, product_id) VALUES (?, ?, ?, ?)";
			$query = $this->db->query($sql, array($name, $name_en, $url, $pro_id));
		}
		function update($name = "", $name_en = "", $url = "", $product_id = -1, $id = -1){
			$sql = "UPDATE `image` SET `name` = ?, `name_en` = ?, `url` = ?, `product_id` = ? ";
			$sql .= "WHERE `id` = ? ";
			$query = $this->db->query($sql, array($name, $name_en, $url, $product_id, $id));
		}
		function remove($id, $url){
			
			$this->db->delete('image', array('id' => $id)); 
			$this->removieImage($url);
		}
		function removieImage($url){
			$path = $this->config->item('product.picture') . $url;
			if(file_exists($path)){
				unlink($path);
			}
		}
		function findByID($id){
			$sql = "SELECT `id`,`name`, `name_en`, `url`, `product_id` FROM `image` WHERE `id` = ? limit 1";
			$query = $this->db->query($sql, array('id'=>$id)); 
			$data = array();
			if($query->num_rows() > 0){
				$data = $query->row_array();
			}
			$query->free_result();
			return $data;
		}
	}
?>